//
//  SampleHandler.m
//  ideashareDemoCapture
//
//  Created by IdeaShare on 2021/1/26.
//  Copyright © 2021 huawei. All rights reserved.
//


#import "SampleHandler.h"
#import "ViewController.h"
#import <IdeaShare_SDK_ReplayKitExt/DataBufferController.h>

static SampleHandler *selfClass = nil;
static long long videoCount  = 0;

@interface SampleHandler ()

@property (nonatomic, assign) long  lastFrameTimestamp;


@property (nonatomic, strong) DataBufferController *dataBufferController;

@end

@implementation SampleHandler

- (void)broadcastStartedWithSetupInfo:(NSDictionary<NSString *,NSObject *> *)setupInfo {
    
    selfClass = self;
    NSString * log_str = @"-----------broadcastStartedWithSetupInfo   start";
    [selfClass printLog:log_str];
    
    _dataBufferController = [[DataBufferController new] initWithGroupId:kGroup];
    
    [self postAndReceiceDarwinNotificationToApp];
    
}

- (void)broadcastFinished {
    CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter ();
    CFNotificationCenterRemoveObserver(notification, (__bridge const void *)(self), CFSTR("extension_setup_info"), NULL);
    //通知demo页面停止发送
    CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter (), CFSTR("extension_stop_info"), NULL,NULL, YES);
}
- (void)postAndReceiceDarwinNotificationToApp {
    NSString * log_str = @"-----------postAndReceiceDarwinNotificationToApp   start";
    [selfClass printLog:log_str];
    CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter ();
    CFNotificationCenterPostNotification(CFNotificationCenterGetDarwinNotifyCenter (), CFSTR("extension_setup_info"), NULL,NULL, YES);
    CFNotificationCenterAddObserver(notification, (__bridge const void *)(self), extensionRecvShareDataStop,CFSTR("app_tell_extension_finished"), NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
}
- (void)processSampleBuffer:(CMSampleBufferRef)sampleBuffer withType:(RPSampleBufferType)sampleBufferType {
    
    switch (sampleBufferType) {
        case RPSampleBufferTypeVideo:
            videoCount += 1;
            [self->_dataBufferController sendVideoBuffer:sampleBuffer];
            break;
        case RPSampleBufferTypeAudioApp:
            [self->_dataBufferController sendAudioBuffer:sampleBuffer];
            break;
        case RPSampleBufferTypeAudioMic:
            break;
        default:
            break;
    }
}

/// 日志打印
/// @param txt 日志字符串
- (void)printLog:(NSString *)txt {
    NSUserDefaults *userDefault = [[NSUserDefaults alloc] initWithSuiteName:kGroup];
    if (userDefault == nil) return;
    [userDefault setValue:txt forKey:@"writeMyLogKey"];
    CFNotificationCenterRef notification = CFNotificationCenterGetDarwinNotifyCenter();
    CFNotificationCenterPostNotification(notification, CFSTR("writeMyLog"), NULL,NULL, YES);
}

void extensionRecvShareDataStop(CFNotificationCenterRef center, void *observer, CFStringRef name, const void *object, CFDictionaryRef userInfo) {
    NSDictionary * userInfotmp = [NSDictionary dictionaryWithObjectsAndKeys:@"", NSLocalizedDescriptionKey, @"投屏中断", NSLocalizedFailureReasonErrorKey, @"",NSLocalizedRecoverySuggestionErrorKey,nil];
    NSError *error = [[NSError alloc] initWithDomain:NSCocoaErrorDomain code:4 userInfo:userInfotmp];
    NSString * log_str = [NSString stringWithFormat:@"【ShareDataStopSyn】 SampleHandler finishBroadcastWithError:%p",selfClass];
    [selfClass printLog:log_str];
    [selfClass finishBroadcastWithError:error];
}

@end
